package com.luo.backend.controller;

import com.luo.backend.common.ApiResponse;
import com.luo.backend.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 测试控制器
 */
@RestController
@RequestMapping("/api/v1/test")
public class TestController {
    
    @Autowired
    private UserRepository userRepository;
    
    /**
     * 健康检查接口
     */
    @GetMapping("/health")
    public ApiResponse<Map<String, Object>> health() {
        Map<String, Object> data = new HashMap<>();
        data.put("status", "UP");
        data.put("service", "美食外卖拼单平台");
        data.put("version", "1.0.0");
        data.put("timestamp", System.currentTimeMillis());
        
        return ApiResponse.success("服务正常运行", data);
    }
    
    /**
     * Hello测试接口
     */
    @GetMapping("/hello")
    public ApiResponse<String> hello() {
        return ApiResponse.success("Hello, 美食外卖拼单平台后端服务！");
    }
    
    /**
     * 数据库连接测试接口
     */
    @GetMapping("/db")
    public ApiResponse<Map<String, Object>> testDatabase() {
        try {
            long userCount = userRepository.count();
            
            Map<String, Object> data = new HashMap<>();
            data.put("database_status", "connected");
            data.put("user_count", userCount);
            data.put("message", "数据库连接正常");
            
            return ApiResponse.success("数据库连接测试成功", data);
        } catch (Exception e) {
            Map<String, Object> data = new HashMap<>();
            data.put("database_status", "error");
            data.put("error_message", e.getMessage());
            
            return ApiResponse.error(500, "数据库连接失败：" + e.getMessage());
        }
    }
}
